import pymysql

#连接数据库
conn = pymysql.Connect(
    host="127.0.0.1",
    port= 3306,
    user="root",
    db ="hqyj",
    password="root",
    charset="utf8"
)
#创建游标对象
cursor = conn.cursor()
#SQL语句执行
sql = "select*from students where cls_id=2;"
ret = cursor.execute(sql)
print(ret)

ret1 = cursor.fetchone()
print("执行一行结果",ret1)
ret2 = cursor.fetchall()
print("执行所有行结果",ret2)
ret3 = cursor.fetchmany(5)
print("执行多行结果",ret3)

sql_1 ="insert into teachers (id,name) values (5,'张老师');"
sql_2 ="insert into teachers (id,name) values (%s,%s);"
# cursor.execute(sql_1)
#cursor.execute(sql_2,[6,"范老师"])

conn.commit()

cursor.close()
conn.close()
